﻿/* 
 Copyright (C) 2011  Amblox Project

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using Amblox.Game;
using Amblox.Encoding;

namespace Amblox.Handlers
{
    public partial class Handler
    {
        private bool MessengerRegistered = false;

        public void InitMessenger()
        {
            if (Messenger != null)
                return;

            Messenger = new Messenger(Habbo);

            FuseBuilder MessengerResponse = new FuseBuilder();
            MessengerResponse.Append(Messenger.SerializeFriends());
            MessengerResponse.AppendChar(1);
            MessengerResponse.Append(Messenger.SerializeRequests());
            Connection.SendData(MessengerResponse);
        }

        public void AcceptFriendRequest()
        {
            if (Messenger == null)
                return;

            int Requests = JeaxEncoding.decodeVL64(Packet.Substring(2));
            int CurLength = 2 + JeaxEncoding.encodeVL64(Requests).Length;

            for (int i = 0; i < Requests; i++)
            {
                int Friend = JeaxEncoding.decodeVL64(Packet.Substring(CurLength));
                CurLength += JeaxEncoding.encodeVL64(Friend).Length;

                Messenger.AcceptRequest(Friend);
            }
        }

        public void RegisterMessenger()
        {
            if (MessengerRegistered)
                return;

            MessengerRegistered = true;

            Packets.Add(12, new Request(InitMessenger));
            Packets.Add(37, new Request(AcceptFriendRequest));
        }
    }
}
